export class StickMan {
  constructor(x, y) {
    this.x = x
    this.y = y
    this.width = 40
    this.height = 80
    this.velocityY = 0
    this.isJumping = false
    this.gravity = 0.5
    this.jumpPower = -12
    this.frames = 0
    this.currentFrame = 0
    this.animationSpeed = 5
    this.maxJump = 3 // 最多三连跳
    this.jumpCount = 0 // 当前已跳次数
  }

  move(dx, dy) {
    this.x += dx
    this.y += dy
    // 边界检查
    if (this.x < 0) this.x = 0
    if (this.x > 800 - this.width) this.x = 800 - this.width
  }

  jump() {
    if (this.jumpCount < this.maxJump) {
      this.velocityY = this.jumpPower
      this.isJumping = true
      this.jumpCount++
    }
  }

  update() {
    // 应用重力
    this.velocityY += this.gravity
    this.y += this.velocityY

    // 地面碰撞检测
    if (this.y > 400) {
      this.y = 400
      this.velocityY = 0
      this.isJumping = false
      this.jumpCount = 0 // 落地时重置跳跃次数
    }

    // 动画帧更新
    this.frames++
    if (this.frames % this.animationSpeed === 0) {
      this.currentFrame = (this.currentFrame + 1) % 4
    }
  }

  draw(ctx) {
    // 绘制火柴人
    ctx.save()
    ctx.translate(this.x + this.width / 2, this.y)

    // 头部
    ctx.beginPath()
    ctx.arc(0, 15, 10, 0, Math.PI * 2)
    ctx.fillStyle = '#333'
    ctx.fill()

    // 身体
    ctx.beginPath()
    ctx.moveTo(0, 25)
    ctx.lineTo(0, 60)
    ctx.strokeStyle = '#333'
    ctx.lineWidth = 3
    ctx.stroke()

    // 手臂 (添加简单动画)
    const armAngle = Math.sin(this.frames * 0.1) * 0.5
    ctx.beginPath()
    ctx.moveTo(0, 30)
    ctx.lineTo(-20 + armAngle * 10, 40)
    ctx.moveTo(0, 30)
    ctx.lineTo(20 - armAngle * 10, 40)
    ctx.stroke()

    // 腿部 (添加跑步动画)
    const legAngle = Math.sin(this.frames * 0.2) * 0.8
    ctx.beginPath()
    ctx.moveTo(0, 60)
    ctx.lineTo(-15 + legAngle * 10, 80)
    ctx.moveTo(0, 60)
    ctx.lineTo(15 - legAngle * 10, 80)
    ctx.stroke()

    ctx.restore()
  }
}
// 在文件底部添加
export default StickMan
